import {
  Constant_Enum_Enum_Order_PayType,
  IConstant_Enum_Type_Order_PayType,
  Constant_Enum_Enum_Order_PayStatus,
  IConstant_Enum_Type_Order_PayStatus,
} from '@/common/constants/enum';
import { UserClient } from '@/modules/businessModule/userClient/assets/userClient.entity';
import { ApiProperty } from '@nestjs/swagger';
import {
  BeforeInsert,
  Column,
  Entity,
  JoinColumn,
  ManyToOne,
  OneToOne,
  PrimaryGeneratedColumn,
} from 'typeorm';
import { Coupon } from '../../coupon/assets/coupon.entity';
import { Rebates } from '../../rebates/assets/rebates.entity';
import { OrderVirtualDetail } from '../../orderVirtualDetail/assets/orderVirtualDetail.entity';

@Entity()
export class Order {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  @ApiProperty({ description: '订单号' })
  orderNumber: string;

  @Column()
  @ApiProperty({
    enum: Constant_Enum_Enum_Order_PayStatus,
    description: '订单状态',
  })
  status: IConstant_Enum_Type_Order_PayStatus;

  @Column({ nullable: true })
  @ApiProperty({ description: '退款备注' })
  refundRemark?: string;

  @Column({ type: 'float' })
  @ApiProperty({ type: 'float', example: 9.5, description: '支付价格' })
  payPrice: number;

  @Column({ nullable: true })
  @ApiProperty({
    enum: Constant_Enum_Enum_Order_PayType,
    description: '支付类型',
  })
  payType?: IConstant_Enum_Type_Order_PayType;

  @Column({
    type: 'bigint',
    nullable: true,
  })
  @ApiProperty({ description: '创建时间' })
  createTime: number;

  @Column({ nullable: true })
  @ApiProperty({ description: '平台单号，如支付宝平台单号' })
  payPlatformTradeNo?: string;

  @ManyToOne(() => UserClient, { cascade: ['remove'], onDelete: 'CASCADE' })
  @ApiProperty({ description: '用户Id', type: () => UserClient })
  @JoinColumn()
  userClient: UserClient;

  @ManyToOne(() => Coupon)
  @ApiProperty({ description: '优惠券', type: () => Coupon })
  @JoinColumn()
  coupon: Coupon;

  @OneToOne(() => Rebates, (rebates) => rebates.order)
  rebates: Rebates;

  @OneToOne(() => OrderVirtualDetail, (orderVirtualDetail) => orderVirtualDetail.order)
  orderVirtualDetail: OrderVirtualDetail;

  @BeforeInsert()
  setCreateTime() {
    this.createTime = Math.floor(Date.now() / 1000); // 使用秒级时间戳
  }
}
